package cn.pegasus.teamwork.pegasus.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.pegasus.teamwork.pegasus.entity.BussCouponInfo;
import cn.pegasus.teamwork.pegasus.mapper.BussCouponInfoMapper;
import cn.pegasus.teamwork.pegasus.service.BussCouponInfoService;
import cn.pegasus.teamwork.util.DateCompareUtil;
import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.iyizhan.teamwork.base.util.PageObject;

/**
 * @author ryan.bian
 * @version 
 */
@Service("bussCouponInfoService")
public class BussCouponInfoServiceImpl extends BaseServiceImpl<BussCouponInfo, BussCouponInfoMapper> implements BussCouponInfoService {
	// 注入当前dao对象
    @Autowired
    private BussCouponInfoMapper bussCouponInfoMapper;

    public BussCouponInfoServiceImpl() {
        setMapperClass(BussCouponInfoMapper.class, BussCouponInfo.class);
    }

	@Override
	public PageObject<BussCouponInfo> PageList(Map<String, Object> map) {
		map.put("sortName", "create_time");
        map.put("sortOrder", "desc");
		return super.Pagequery(map);
	}

	@Override
	public void addCoupon(Long userId, BussCouponInfo info) {
		info.setCreateTime(new Date());
		info.setCreateUserBy(userId);
		info.setUpdateTime(new Date());
		info.setUpdateUserBy(userId);
		info.setCcouponStatus(0);
		Long id = bussCouponInfoMapper.getCouponId();
		if(null != id){
			id = id + 1L;
		}
		info.setCouponCode(DateCompareUtil.getStrDate(new Date(),"yyyyMMssHH")+id);
		bussCouponInfoMapper.insertByEntity(info);
	}

	@Override
	public void updateCoupon(Long userId, BussCouponInfo info) {
		info.setUpdateTime(new Date());
		info.setUpdateUserBy(userId);
		bussCouponInfoMapper.updateByEntity(info);
	}

	@Override
	public void delete(Long userId, Long id) {
		BussCouponInfo info = new BussCouponInfo();
		info.setId(id);
		info.setUpdateTime(new Date());
		info.setUpdateUserBy(userId);
		info.setCcouponStatus(3);
		bussCouponInfoMapper.updateByEntity(info);
	}

	@Override
	public BussCouponInfo couponInfo(Long id) {
		BussCouponInfo info = new BussCouponInfo();
		info.setId(id);
		BussCouponInfo coupon = bussCouponInfoMapper.selectSingle(info);
		if(coupon.getCcouponUsetype() == 2){
			String time = coupon.getCcouponUseparam();
			String[] str = time.split(",");
			coupon.setParamStart(str[0]);
			coupon.setParamEnd(str[1]);
			coupon.setCcouponUseparam("");
		}
		return coupon;
	}
}
